package com.cn.boot.student.controller.rbac;

import com.cn.boot.student.entity.RolePermissionRelationEntity;
import com.cn.boot.student.service.rbac.IRolePermissionService;
import com.cn.boot.student.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色权限管理控制器
 */
@RestController
@RequestMapping("/admin/rolePermission")
@Api(tags = "角色权限管理控制器")
public class RolePermissionRelationController {
    @Autowired
    private IRolePermissionService iRolePermissionService;

    /**
     * 通过角色id得到该角色所拥有的权限信息
     * @param id
     * @return
     */
    @PostMapping("/getIsPermissionByRoleId")
    @PreAuthorize("hasAuthority('sys:permission:read')")
    @ApiOperation(value ="通过角色id得到该角色所拥有的权限信息" )
    public CommonResult getIsPermissionByRoleId(Integer id){
        return iRolePermissionService.getIsPermissionByRoleId(id);
    }
    /**
     * 通过角色id得到该角色没有的权限信息
     * @param id
     * @return
     */
    @PostMapping("/getNoPermissionByRoleId")
    @PreAuthorize("hasAuthority('sys:permission:read')")
    @ApiOperation(value ="通过角色id得到该角色没有的权限信息" )
    public CommonResult getNoPermissionByRoleId(Integer id){
        return iRolePermissionService.getNoPermissionByRoleId(id);
    }
    /**
     * 通过角色id,权限名得到该角色拥有的权限信息
     *
     * @param entity
     * @return
     */

    @PostMapping("/getIsPermissionByRoleIdAndPermissionName")
    @PreAuthorize("hasAuthority('sys:permission:read')")
    @ApiOperation(value ="通过角色id,权限名得到该角色拥有的权限信息" )
    public CommonResult getIsPermissionByRoleIdAndPermissionName(RolePermissionRelationEntity entity){
        return iRolePermissionService.getIsPermissionByRoleIdAndPermissionName(entity);
    }

    /**
     * 通过角色id,权限名得到该角色拥有的权限信息
     *
     * @param entity
     * @return
     */

    @PostMapping("/getNoPermissionByRoleIdAndPermissionName")
    @PreAuthorize("hasAuthority('sys:permission:read')")
    @ApiOperation(value ="通过角色id,权限名得到该角色未拥有的权限信息" )
    public CommonResult getNoPermissionByRoleIdAndPermissionName(RolePermissionRelationEntity entity){
        return iRolePermissionService.getNoPermissionByRoleIdAndPermissionName(entity);
    }

    /**
     * 分配权限
     * @param entities
     * @return
     */
    @PostMapping("/adds")
    @PreAuthorize("hasAuthority('sys:permission:add')")
    @ApiOperation(value ="分配权限" )
    public CommonResult adds(@RequestBody List<RolePermissionRelationEntity> entities ){
        return iRolePermissionService.adds(entities);
    }

    /**
     *删除权限
     * @param entities
     * @return
     */
    @DeleteMapping("/dels")
    @PreAuthorize("hasAuthority('sys:permission:del')")
    @ApiOperation(value ="删除权限" )
    public CommonResult dels(@RequestBody List<RolePermissionRelationEntity> entities ){
        return iRolePermissionService.dels(entities);
    }
}
